<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic|Microsoft YaHei:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="java,spring-mvc," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="preludeAs we all know, interceptor in spring-mvc is very useful to address various needs,one need is How to mark the request log more compact.
In our project the request log will record all the html p">
<meta property="og:type" content="article">
<meta property="og:title" content="HandlerInterceptorAdapter">
<meta property="og:url" content="http://yoursite.com/2017/02/16/spring-mvc-interceptor/index.html">
<meta property="og:site_name" content="Arvin Alfod">
<meta property="og:description" content="preludeAs we all know, interceptor in spring-mvc is very useful to address various needs,one need is How to mark the request log more compact.
In our project the request log will record all the html p">
<meta property="og:updated_time" content="2017-02-25T13:59:19.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HandlerInterceptorAdapter">
<meta name="twitter:description" content="preludeAs we all know, interceptor in spring-mvc is very useful to address various needs,one need is How to mark the request log more compact.
In our project the request log will record all the html p">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: 'Author'
    }
  };
</script>

  <title> HandlerInterceptorAdapter | Arvin Alfod </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Arvin Alfod</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                HandlerInterceptorAdapter
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2017-02-16T23:08:36+08:00" content="2017-02-16">
              2017-02-16
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Technology/" itemprop="url" rel="index">
                    <span itemprop="name">Technology</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="prelude"><a href="#prelude" class="headerlink" title="prelude"></a>prelude</h1><p>As we all know, interceptor in spring-mvc is very useful to address various needs,<br>one need is How to mark the request log more compact.</p>
<p>In our project the request log will record all the html page which made it very large<br>and after about two years running, the server space is about to be filled up.</p>
<p>One solution is to buy greater capacity of server space, but it is not the best way.<br>though the whole html file is large, but the useful infomation density is low, actually,<br>the ultimate aim of us is to record the ModelAndView instance, not the whole output page.<br>so I use HandlerInterceptorAdapter to get ModelAndView instance;</p>
<p><code>org.springframework.web.servlet.handler.HandlerInterceptorAdapter</code> has some functions to help developer to control the flow and data of web service. one of them is <code>void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)</code> which will be called between a ModelAndView instance builded completed by controller layer and be rendered by view layer. so you can do something at an architectural level in this process, which is we need.</p>
<h1 id="configuration"><a href="#configuration" class="headerlink" title="configuration"></a>configuration</h1><p>this is the Spring-mvc interceptor configuration<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">	&lt;mvc:interceptor&gt;</div><div class="line">		&lt;mvc:mapping path=&quot;/*&quot; /&gt;</div><div class="line">     &lt;!-- request of static resource don`t has ModelAndView instance  --&gt;</div><div class="line">		&lt;mvc:exclude-mapping path=&quot;/static/*&quot; /&gt;</div><div class="line">		&lt;bean</div><div class="line">				class=&quot;*.LoggingResponse&quot; /&gt;</div><div class="line">	&lt;/mvc:interceptor&gt;</div><div class="line">&lt;/mvc:interceptors&gt;</div></pre></td></tr></table></figure></p>
<p>we get the ModelAndView instance in spring-mvc interceptor, and record it in filter,<br>out of system maintainability</p>
<h1 id="code"><a href="#code" class="headerlink" title="code"></a>code</h1><h2 id="postHandle"><a href="#postHandle" class="headerlink" title="postHandle"></a>postHandle</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">public class LoggingResponse extends HandlerInterceptorAdapter &#123;</div><div class="line">    //to save and transmitting the ModelAndView instance</div><div class="line">    public static ThreadLocal&lt;String&gt; threadLocal = new ThreadLocal&lt;&gt;();</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void postHandle(HttpServletRequest request,</div><div class="line">                           HttpServletResponse response,</div><div class="line">                           Object handler, ModelAndView modelAndView) throws Exception &#123;</div><div class="line"></div><div class="line">        if (modelAndView != null) &#123;</div><div class="line">            threadLocal.set(modelAndView.toString());</div><div class="line">        &#125;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">private void logResponse(final ResponseWrapper response) &#123;</div><div class="line">        StringBuilder msg = new StringBuilder();</div><div class="line">        msg.append(RESPONSE_PREFIX);</div><div class="line">        msg.append(&quot;request id=&quot;).append(response.getId());</div><div class="line"></div><div class="line">        String modelAndView = threadLocal.get();</div><div class="line">        if (null != modelAndView) &#123;</div><div class="line">            msg.append(&quot;;&quot;).append(modelAndView);</div><div class="line">        &#125; else &#123;</div><div class="line">            msg.append(&quot;;modelAndView=null&quot;);</div><div class="line">        &#125;</div><div class="line">        logger.info(msg.toString());</div><div class="line">        //destroyed the threadLocal manually to prevent memory leak</div><div class="line">        threadLocal.remove();</div><div class="line"></div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<h1 id="more"><a href="#more" class="headerlink" title="more"></a>more</h1><p>by the way, I list the other functions of HandlerInterceptorAdapter to<br>facilitate you.</p>
<h2 id="preHandle"><a href="#preHandle" class="headerlink" title="preHandle"></a>preHandle</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">public boolean preHandle(HttpServletRequest request,</div><div class="line">                         HttpServletResponse response,</div><div class="line">                         Object handler)</div><div class="line">                  throws Exception</div></pre></td></tr></table></figure>
<p>true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.</p>
<h2 id="afterCompletion"><a href="#afterCompletion" class="headerlink" title="afterCompletion"></a>afterCompletion</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">public void afterCompletion(HttpServletRequest request,</div><div class="line">                            HttpServletResponse response,</div><div class="line">                            Object handler,</div><div class="line">                            Exception ex)</div><div class="line">                     throws Exception</div></pre></td></tr></table></figure>
<p>this function will be called on the completion of the view, of course, the preHandle’s return value muse be true.</p>
<h2 id="afterConcurrentHandlingStarted"><a href="#afterConcurrentHandlingStarted" class="headerlink" title="afterConcurrentHandlingStarted"></a>afterConcurrentHandlingStarted</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">public void afterConcurrentHandlingStarted(HttpServletRequest request,</div><div class="line">                                           HttpServletResponse response,</div><div class="line">                                           Object handler)</div><div class="line">                                    throws Exception</div></pre></td></tr></table></figure>
<p>this function will be called on the start of controller is being called in asynchronous state.</p>

      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/java/" rel="tag">#java</a>
          
            <a href="/tags/spring-mvc/" rel="tag">#spring-mvc</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/02/25/research-in-java-class-file/" rel="prev" title="research in java class file">
                research in java class file <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avator.jpg"
               alt="Arvin Alfod" />
          <p class="site-author-name" itemprop="name">Arvin Alfod</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">2</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/arvin.alfod" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://github.com/alfod" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/arvinalfod" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.quora.com/profile/Arvin-Alfod" target="_blank" title="Quora">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Quora
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#prelude"><span class="nav-number">1.</span> <span class="nav-text">prelude</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#configuration"><span class="nav-number">2.</span> <span class="nav-text">configuration</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#code"><span class="nav-number">3.</span> <span class="nav-text">code</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#postHandle"><span class="nav-number">3.1.</span> <span class="nav-text">postHandle</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#more"><span class="nav-number">4.</span> <span class="nav-text">more</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#preHandle"><span class="nav-number">4.1.</span> <span class="nav-text">preHandle</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#afterCompletion"><span class="nav-number">4.2.</span> <span class="nav-text">afterCompletion</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#afterConcurrentHandlingStarted"><span class="nav-number">4.3.</span> <span class="nav-text">afterConcurrentHandlingStarted</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2016-12 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Arvin Alfod</span>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  



  
  
  

  

  

</body>
</html>
